/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominating.species.entities;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import pa165.dominating.species.dao.PersistObject;

/**
 *
 * @author Filip Dinh
 */
@Entity
public class Animal implements Serializable, PersistObject {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String species;
    private String name;
    @ManyToOne(fetch = FetchType.EAGER)
    private Environment environment;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "PREDATOR_PREY",
            joinColumns = {
        @JoinColumn(name = "PREDATOR_ID", referencedColumnName = "ID")},
            inverseJoinColumns = {
        @JoinColumn(name = "PREY_ID", referencedColumnName = "ID")})
    private Set<Animal> prey = new HashSet<>();

    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void addPrey(Animal prey) {
        this.prey.add(prey);
    }

    public void removePrey(Animal prey) {
        this.prey.remove(prey);
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    public void removeEnvironment(Environment environment) {
        this.environment = null;
    }

    public String getSpecies() {
        return species;
    }

    public String getName() {
        return name;
    }

    public Environment getEnvironment() {
        return environment;
    }

    public Set<Animal> getPrey() {
        return prey;
    }

    public void setPrey(Set<Animal> prey) {
        this.prey = prey;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Animal)) {
            return false;
        }
        Animal other = (Animal) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Animal " + this.id + " " + this.species + " " + this.name + " " + this.environment;
    }
}
